home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / crypt / crypt_device_1_8.lha / cryptdev.a < prev    next >
Text File  |  1992-10-25  |  21KB  |  1,201 lines

  1. ;
  2. ; cryptdev.a   --   created from fdev.device -- "filesystem in a file"
  3. ;
  4. ; 1992-10-14
  5. ;
  6. ; uses IDEA encryption algorithm
  7. ;
  8. ;
  9. ; created  1989-08-13  TR
  10. ;
  11. ; modifications:
  12. ;  1989-08-15  TR  -  added separate dos calling process
  13. ;  1989-08-16  TR  -  removed unit processes (all commands are IMMEDIATE...)
  14. ;  1989-08-19  TR  -  changed DosCaller parameter passing
  15. ;              also no longer need to RemTask a process
  16. ;              dos caller process messages now contain IORequest
  17. ;              pointer in LN_NAME field. zero LN_NAME field
  18. ;              means that the message is a request to quit
  19. ;              the DosCaller process.
  20. ;  1989-08-20  TR  -  now closes the file when requested to turn motor off
  21. ;  1989-08-31  TR  -  some small bugs fixed. AbortIO now returns an error.
  22. ;
  23. ;  1991-06-10  TR  -  Now properly clears IO_ERROR when there is no error.
  24. ;              Can now be assembled with standard include files.
  25. ;              Created Makefile (for PD Make & A68k)
  26. ;
  27.  
  28.     nolist
  29.     include    'exec/types.i'
  30.     include    'exec/initializers.i'
  31.     include    'exec/memory.i'
  32.     include    'exec/io.i'
  33.     include    'exec/devices.i'
  34.     include    'exec/resident.i'
  35.     include    'exec/errors.i'
  36.     include    'devices/trackdisk.i'
  37.     include    'libraries/dos.i'
  38.     include    'libraries/dosextens.i'
  39.     include    'intuition/intuition.i'
  40.     list
  41. ;
  42. ; new library call macros
  43. ;
  44. getbase        macro
  45.         ifc    '\1','Exec'
  46.           ifeq    NARG-2
  47.             move.l _ExecBase,\2
  48.           endc
  49.           ifeq    NARG-1
  50.             move.l _ExecBase,a6
  51.           endc
  52.         endc
  53.         ifnc    '\1','Exec'
  54.           ifeq    NARG-2
  55.             move.l _\1Base(a4),\2
  56.           endc
  57.           ifeq    NARG-1
  58.             move.l _\1Base(a4),a6
  59.           endc
  60.         endc
  61.         endm
  62.  
  63. lib        macro
  64.         ifeq    NARG-2
  65.         getbase    \1
  66.         lib    \2
  67.         endc
  68.         ifeq    NARG-1
  69.         xref    _LVO\1
  70.         jsr    _LVO\1(a6)
  71.         endc
  72.         endm
  73.  
  74. jlib        macro
  75.         ifeq    NARG-2
  76.         getbase    \1
  77.         jlib    \2
  78.         endc
  79.         ifeq    NARG-1
  80.         jmp    _LVO\1(a6)
  81.         endc
  82.         endm
  83.  
  84. slib        macro
  85.         move.l    a6,-(sp)
  86.         lib    \1,\2
  87.         move.l    (sp)+,a6
  88.         endm
  89.  
  90. xlib        macro
  91.         xref    _LVO\1
  92.         endm
  93.  
  94. rw        macro
  95.         dc.w    \1-*
  96.         endm
  97.  
  98. _ExecBase    equ    4
  99. ;
  100. ;
  101.  
  102.  
  103. ;
  104. ; external references for encryption functions (in idea.c)
  105. ;
  106.         xref    _idea_ecb_encrypt
  107.         xref    _idea_cbc_encrypt
  108.         xref    _idea_key_schedule
  109.  
  110. BLOCKSIZE    equ    512
  111. KS_SIZE        equ    216
  112.  
  113. ;
  114. ; the data structure of the device
  115. ;
  116.  
  117.     STRUCTURE CryptDevBase,LIB_SIZE
  118.      APTR    crd_SegList
  119.      APTR    crd_DosLib
  120.      APTR    crd_IntuitionBase
  121.      APTR    crd_DosCallerProc
  122.      APTR    crd_DosCallerPort
  123.      STRUCT    crd_DosCallerInitMsg,MN_SIZE
  124.      STRUCT    crd_UnitList,MLH_SIZE
  125.     LABEL crd_Sizeof
  126.  
  127. ;
  128. ; unit data structure. note that units are linked to the device
  129. ; structure using the crdu_Link field.
  130. ;
  131.     STRUCTURE CryptDevUnit,UNIT_SIZE
  132.      STRUCT    crdu_Link,MLN_SIZE    to link to units to a list
  133.      UWORD    crdu_UnitNum
  134.      LONG    crdu_FileHandle        zero if file not open
  135.      STRUCT    crdu_FileName,32
  136.      STRUCT crdu_KeySched,KS_SIZE
  137.      STRUCT    crdu_IVec,8
  138.      STRUCT    crdu_Buffer,BLOCKSIZE
  139.      UBYTE    crdu_KeyValid
  140.     LABEL crdu_Sizeof
  141.  
  142. DOSCALLERSTACK    equ    2000
  143. DOSCALLERPRI    equ    0
  144.  
  145. ;
  146. ; try to remember to increment the revision number...
  147. ;
  148. VERSION        equ    1
  149. REVISION    equ    8
  150.  
  151.  
  152.     section    text,CODE
  153.  
  154. ;
  155. ; start of code -- just return zero if someone executes this directly
  156. ; the Null-standard library routine points also here
  157. ;
  158. Null    moveq    #0,d0
  159.     rts
  160.  
  161. RomTag    dc.w    RTC_MATCHWORD
  162.     dc.l    RomTag
  163.     dc.l    EndSkip
  164.     dc.b    RTF_AUTOINIT
  165.     dc.b    VERSION
  166.     dc.b    NT_DEVICE
  167.     dc.b    0
  168.     dc.l    dev_name
  169.     dc.l    dev_idstring
  170.     dc.l    dev_init
  171.  
  172. dev_name    dc.b    'crypt.device',0
  173. dev_idstring    dc.b    'crypt 1.8 (TR 16-Oct-1992)',13,10,0
  174.  
  175. dosname        dc.b    'dos.library',0
  176. intuition_name    dc.b    'intuition.library',0
  177.  
  178. doscallername    dc.b    'CryptDev_DosCaller',0
  179.  
  180.     even
  181. ;
  182. ; because the RTF_AUTOINIT flag in the RomTag structure was set
  183. ; RT_INIT points to this table of parameters for MakeLibrary()
  184. ;
  185. dev_init
  186.     dc.l    crd_Sizeof
  187.     dc.l    dev_FuncInit
  188.     dc.l    dev_StructInit
  189.     dc.l    dev_InitRoutine
  190.  
  191. dev_StructInit
  192.     INITBYTE LN_TYPE,NT_DEVICE
  193.     INITLONG LN_NAME,dev_name
  194.     INITLONG LIB_IDSTRING,dev_idstring
  195.     INITBYTE LIB_FLAGS,LIBF_CHANGED!LIBF_SUMUSED
  196.     INITLONG LIB_VERSION,(VERSION<<16)+REVISION
  197.     dc.l    0
  198.  
  199. ;
  200. ; a simple macro to save typing in library routine definitions below
  201. ;
  202. fw    macro
  203.     dc.w    \1-dev_FuncInit
  204.     endm
  205.  
  206. dev_FuncInit
  207.     dc.w    -1
  208. ; standard system library routines
  209.     fw    Open
  210.     fw    Close
  211.     fw    Expunge
  212.     fw    Null
  213. ; standard device routines
  214.  
  215.     fw    BeginIO
  216.     fw    AbortIO
  217.  
  218. ; end of table marker
  219.     dc.w    -1
  220.  
  221. ;
  222. ; device command table
  223. ;
  224. DevCmdTable
  225.     rw    Invalid            CMD_INVALID
  226.     rw    ResetPassword        CMD_RESET
  227.     rw    DoRead            CMD_READ
  228.     rw    DoWrite            CMD_WRITE
  229.     rw    DoNothing        CMD_UPDATE
  230.     rw    DoNothing        CMD_CLEAR
  231.     rw    Invalid            CMD_STOP
  232.     rw    Invalid            CMD_START
  233.     rw    DoNothing        CMD_FLUSH
  234.     rw    DoMotor            TD_MOTOR
  235.     rw    DoNothing        TD_SEEK
  236.     rw    DoWrite            TD_FORMAT
  237.     rw    DoNothing        TD_REMOVE
  238.     rw    DoNothing        TD_CHANGENUM
  239.     rw    DoNothing        TD_CHANGESTATE
  240.     rw    DoNothing        TD_PROTSTATUS
  241.     rw    Invalid            TD_RAWREAD
  242.     rw    Invalid            TD_RAWWRITE
  243.     rw    DoGetDriveType        TD_GETDRIVETYPE
  244.     rw    DoGetNumTracks        TD_GETNUMTRACKS
  245.     rw    DoNothing        TD_ADDCHANGEINT
  246.     rw    DoNothing        TD_REMCHANGEINT
  247.  
  248. ;
  249. ; init routine, entry: d0=LibBase, a0=SegList, a6=ExecBase
  250. ; return: d0=LibBase if initialization is successfull, zero if failed
  251. ;
  252. dev_InitRoutine
  253.     move.l    a4,-(sp)
  254.     move.l    d0,a4            get library base in a4
  255.     move.l    a0,crd_SegList(a4)    save our seglist for expunge...
  256.  
  257. ;
  258. ; allocate and partially initialize the dos caller process message port
  259. ; the dos caller process initializes MP_SIGBIT and MP_SIGTASK fields
  260. ; and sets MP_FLAGS to PA_SIGNAL...
  261. ;
  262.     moveq    #MP_SIZE,d0
  263.     move.l    #MEMF_CLEAR!MEMF_PUBLIC,d1
  264.     lib    AllocMem
  265.     move.l    d0,crd_DosCallerPort(a4)
  266.     beq    initfail3
  267.     move.l    d0,a0
  268.     move.b    #PA_IGNORE,MP_FLAGS(a0)
  269.     lea    MP_MSGLIST(a0),a0
  270.     NEWLIST    a0
  271.  
  272.     lea    intuition_name(pc),a1
  273.     moveq    #33,d0
  274.     lib    OpenLibrary
  275.     move.l    d0,crd_IntuitionBase(a4)
  276.     beq.b    initfail2a
  277.  
  278.     lea    dosname(pc),a1
  279.     moveq    #33,d0
  280.     lib    OpenLibrary
  281.     move.l    d0,crd_DosLib(a4)
  282.     beq.s    initfail2
  283.     move.l    a6,-(sp)
  284.     move.l    d0,a6
  285.  
  286.     move.l    #DOSCALLERSTACK,d4
  287.     lea    doscallerseglist(pc),a0
  288.     move.l    a0,d3
  289.     lsr.l    #2,d3            convert to BPTR
  290.     moveq    #DOSCALLERPRI,d2
  291.     lea    doscallername(pc),a0
  292.     move.l    a0,d1
  293.     lib    CreateProc
  294.     move.l    (sp)+,a6
  295.     move.l    d0,crd_DosCallerProc(a4)
  296.     tst.l    d0
  297.     beq.s    initfail1
  298.  
  299. ;
  300. ; send startup message to the dos caller process
  301. ;
  302.     move.l    d0,a0
  303.     lea    crd_DosCallerInitMsg(a4),a1
  304.     move.l    a4,LN_NAME(a1)
  305.     lib    PutMsg
  306.  
  307.     lea    crd_UnitList(a4),a0
  308.     NEWLIST    a0
  309.  
  310.     move.l    a4,d0
  311.     bra.s    init9
  312.  
  313. initfail1
  314.     move.l    crd_DosLib(a4),a1
  315.     lib    CloseLibrary
  316. initfail2
  317.     move.l    crd_IntuitionBase(a4),a1
  318.     lib    CloseLibrary
  319. initfail2a
  320.     move.l    crd_DosCallerPort(a4),a1
  321.     moveq    #MP_SIZE,d0
  322.     lib    FreeMem
  323. initfail3
  324.     moveq    #0,d0
  325.  
  326. init9    move.l    (sp)+,a4
  327.     rts
  328.  
  329. ;
  330. ; open routine, entry: a6=DevBase, a1=IORequest, d0=UnitNum, d1=Flags
  331. ; return: d0=zero or error code
  332. ;
  333. Open    movem.l    d2/a2-a4,-(sp)
  334.     move.l    a1,a2
  335.  
  336.     move.l    d0,d2        save unit number
  337.     bsr    FindUnit
  338.     tst.l    d0
  339.     bne.s    unit_ready
  340.  
  341.     move.l    d2,d0
  342.     bsr    InitUnit
  343.     tst.l    d0
  344.     beq.s    open_err
  345.  
  346. unit_ready
  347.     move.l    d0,a3
  348.     move.l    d0,IO_UNIT(a2)
  349.  
  350.     addq.w    #1,UNIT_OPENCNT(a3)
  351.     addq.w    #1,LIB_OPENCNT(a6)
  352.     and.b    #~LIBF_DELEXP,LIB_FLAGS(a6)
  353.     moveq    #0,d0
  354.  
  355. open_exit
  356.     movem.l    (sp)+,d2/a2-a4
  357.     rts
  358.  
  359. open_err
  360.     moveq    #IOERR_OPENFAIL,d0
  361.     move.b    d0,IO_ERROR(a2)
  362.     bra.s    open_exit
  363.  
  364. ;
  365. ; close routine, entry: a6=devBase, a1=IORequest
  366. ; return: d0=SegList if device no longer in use, else zero
  367. ;
  368. Close    movem.l    a2-a3,-(sp)
  369.     move.l    a1,a2
  370.  
  371.     move.l    IO_UNIT(a2),a3
  372.  
  373.     moveq    #-1,d0
  374.     move.l    d0,IO_DEVICE(a2)
  375.     move.l    d0,IO_UNIT(a2)
  376.  
  377.     subq.w    #1,UNIT_OPENCNT(a3)
  378.     bne.s    do_close
  379.     bsr    ExpungeUnit
  380.  
  381. do_close
  382.     moveq    #0,d0
  383.     subq.w    #1,LIB_OPENCNT(a6)
  384.     bne.s    100$
  385.     btst    #LIBB_DELEXP,LIB_FLAGS(a6)
  386.     beq.s    100$
  387.     bsr.s    Expunge
  388.  
  389. 100$    movem.l    (sp)+,a2-a3
  390.     rts
  391.  
  392. ;
  393. ; expunge routine, entry: a6=devBase
  394. ; return: d0=SegList if device no longer in use, else zero
  395. ;
  396. Expunge    tst.w    LIB_OPENCNT(a6)
  397.     beq.s    200$
  398.     or.b    #LIBF_DELEXP,LIB_FLAGS(a6)
  399.     moveq    #0,d0
  400.     rts
  401. 200$    movem.l    a4-a6,-(sp)
  402.     move.l    a6,a4
  403.     move.l    _ExecBase,a6
  404.     move.l    crd_SegList(a4),a5
  405.     move.l    a4,a1
  406.     lib    Remove
  407.  
  408. ;
  409. ; send termination message to dos caller process
  410. ;
  411.     suba.l    a1,a1
  412.     move.l    a4,a6
  413.     bsr    SendDosCallerRequest
  414.  
  415.     move.l    crd_DosCallerPort(a4),a1
  416.     moveq    #MP_SIZE,d0
  417.     lib    Exec,FreeMem
  418.  
  419.     move.l    crd_DosLib(a4),a1
  420.     lib    CloseLibrary
  421.  
  422.     move.l    crd_IntuitionBase(a4),a1
  423.     lib    CloseLibrary
  424.  
  425.     move.l    a4,a1
  426.     moveq    #0,d0
  427.     moveq    #0,d1
  428.     move.w    LIB_NEGSIZE(a4),d0
  429.     move.w    LIB_POSSIZE(a4),d1
  430.     sub.l    d0,a1
  431.     add.l    d1,d0
  432.     lib    FreeMem
  433.     move.l    a5,d0
  434.     movem.l    (sp)+,a4-a6
  435.     rts
  436.  
  437. ;
  438. ; find unit with a given unit number
  439. ; inputs: a6=DevBase, d0=UnitNum
  440. ; returns: d0=pointer to unit or zero if not found
  441. FindUnit
  442.     move.l    crd_UnitList(a6),a0
  443. 00$    move.l    (a0),d1
  444.     beq.s    01$
  445.     cmp.w    crdu_UnitNum-crdu_Link(a0),d0
  446.     beq.s    02$
  447.     move.l    d1,a0
  448.     bra.s    00$
  449. 01$    moveq    #0,d0
  450.     rts
  451. 02$    lea    -crdu_Link(a0),a0
  452.     move.l    a0,d0
  453.     rts
  454.  
  455. ;
  456. ; initialize an unit. a6=DevBase, d2=unitnum
  457. ;
  458. InitUnit
  459.     movem.l    d2-d4/a3,-(sp)
  460.  
  461.     move.l    #crdu_Sizeof,d0
  462.     move.l    #MEMF_PUBLIC!MEMF_CLEAR,d1
  463.     slib    Exec,AllocMem
  464.     tst.l    d0
  465.     beq.s    init_u9
  466.     move.l    d0,a3
  467.     move.w    d2,crdu_UnitNum(a3)
  468.  
  469. ; create filename for FDEV-file
  470.     movem.l    a2-a3/a6,-(sp)
  471.     lea    formatstr(pc),a0
  472.     lea    crdu_UnitNum(a3),a1
  473.     lea    putch(pc),a2
  474.     lea    crdu_FileName(a3),a3
  475.     lib    Exec,RawDoFmt
  476.     movem.l    (sp)+,a2-a3/a6
  477.  
  478. ;
  479. ; (encryption key is asked from the user and initialized
  480. ; in DosCallerProc if crdu_KeyValid is false...)
  481. ;
  482.  
  483.     lea    crd_UnitList(a6),a0
  484.     lea    crdu_Link(a3),a1
  485.     slib    Exec,AddHead
  486.     move.l    a3,d0
  487.  
  488. init_u9    movem.l    (sp)+,d2-d4/a3
  489.     rts
  490.  
  491. putch    move.b    d0,(a3)+
  492.     rts
  493.  
  494. formatstr    dc.b    'CRDEV%d:',0
  495.  
  496.     even
  497. ;
  498. ; expunge an unit. a3=unit, a6=device
  499. ;
  500. ExpungeUnit
  501. ;
  502. ; request dos caller process to close the file. we give it a fake
  503. ; iorequest with io_Command set to CMD_RESET. we use the crdu_FileName
  504. ; field of the unit structure to store that iorequest (it is not used
  505. ; anymore anyway...)
  506. ;
  507.     lea    crdu_FileName(a3),a1
  508.     move.l    a3,IO_UNIT(a1)            this is important
  509.     move.w    #CMD_RESET,IO_COMMAND(a1)
  510.     bsr    SendDosCallerRequest
  511.  
  512.     move.l    a6,-(sp)
  513.     move.l    _ExecBase,a6
  514.  
  515.     lea    crdu_Link(a3),a1
  516.     lib    Remove
  517.  
  518.     move.l    a3,a1
  519.     move.l    #crdu_Sizeof,d0
  520.     lib    FreeMem
  521.  
  522.     move.l    (sp)+,a6
  523.     rts
  524.  
  525. ;
  526. ; a1=IORequest, a6=Device
  527. ;
  528. BeginIO    movem.l    d2/a2-a3,-(sp)    
  529.     move.l    a1,a2
  530.     clr.b    IO_ERROR(a2)
  531.     move.l    IO_UNIT(a2),a3
  532.     move.w    IO_COMMAND(a2),d2
  533.     and.w    #$7fff,d2
  534.     cmp.w    #TD_LASTCOMM,d2
  535.     bcc.s    invalid_cmd
  536.  
  537. ;beginio_immediate
  538. ;PerformIO    ; a2=IORequest, a3=Unit, a6=Device
  539.     add.w    d2,d2
  540.     lea    DevCmdTable(pc),a0
  541.     add.w    d2,a0
  542.     add.w    (a0),a0
  543.     jsr    (a0)
  544.  
  545. beginio_exit
  546.     movem.l    (sp)+,d2/a2-a3
  547.     rts
  548.  
  549. invalid_cmd
  550.     move.b    #IOERR_NOCMD,IO_ERROR(a1)
  551.     bra.s    beginio_exit
  552.  
  553. ; you can't abort this
  554. AbortIO    moveq    #IOERR_NOCMD,d0
  555.     rts
  556.  
  557. InvalidParams
  558.     move.b    #IOERR_BADLENGTH,IO_ERROR(a1)
  559.     bra.b    TermIO
  560.  
  561.     ; a1=IORequest, a6=Device
  562. Invalid    move.b    #IOERR_NOCMD,IO_ERROR(a1)
  563.     ; fall to TermIO
  564. DoNothing
  565.     clr.l    IO_ACTUAL(a1)
  566. TermIO        ; a1=IORequest, a3=Unit, a6=Device
  567. ;termio_immediate
  568.     btst    #IOB_QUICK,IO_FLAGS(a1)
  569.     bne.s    termio_exit
  570.  
  571.     slib    Exec,ReplyMsg
  572.  
  573. termio_exit
  574.     rts
  575.  
  576. ;
  577. ; here are the device command routines
  578. ;  a1=a2=IORequest
  579. ;  a3=Unit
  580. ;  a6=Device
  581. ;
  582.  
  583. DoGetDriveType
  584.     moveq    #1,d0            make it look like 3.5"
  585.     move.l    d0,IO_ACTUAL(a1)
  586.     bra    TermIO
  587.  
  588. ;
  589. ; actually any number will do,
  590. ; this device really doesn't even know it's size...
  591. ;
  592. DoGetNumTracks
  593.     move.l    #80,IO_ACTUAL(a1)
  594.     bra    TermIO
  595.  
  596. ;
  597. ; CMD_RESET, clears password
  598. ;
  599. ResetPassword
  600.     lea    crdu_KeySched(a3),a0
  601.     moveq    #(KS_SIZE/4)-1,d1
  602.     moveq    #0,d0
  603. 1$    move.l    d0,(a0)+
  604.     dbf    d1,1$
  605.     clr.b    crdu_KeyValid(a3)
  606.     bra    TermIO
  607.  
  608. ;
  609. ; this does not work exactly like trackdisk TD_MOTOR but hopefully
  610. ; the file system doesn't mind...
  611. ;
  612. DoMotor    moveq    #0,d0
  613.     tst.l    crdu_FileHandle(a3)
  614.     sne    d0
  615.     move.l    d0,IO_ACTUAL(a1)
  616.     tst.l    IO_LENGTH(a1)
  617.     bne.s    01$
  618.     move.b    #CMD_RESET,IO_COMMAND+1(a1)
  619.     bsr.s    SendDosCallerRequest    request to close the file
  620.     move.b    #TD_MOTOR,IO_COMMAND+1(a2)
  621. 01$    move.l    a2,a1
  622.     bra    TermIO
  623.  
  624. DoRead
  625. DoWrite    ;check that IO_OFFSET and IO_LENGTH are divisible by BLOCKSIZE (512)
  626.     move.l    IO_OFFSET(a1),d0
  627.     and.w    #BLOCKSIZE-1,d0
  628.     bne.b    InvalidParams
  629.     move.l    IO_LENGTH(a1),d0
  630.     and.w    #BLOCKSIZE-1,d0
  631.     bne    InvalidParams
  632.  
  633.     bsr.s    SendDosCallerRequest
  634.     move.l    a2,a1
  635.     bra    TermIO
  636.  
  637. ;
  638. ; send requests to DosCallerProc
  639. ;
  640. SendDosCallerRequest
  641.     movem.l    d5/a2/a5-a6,-(sp)
  642.     move.l    a1,d5
  643.  
  644.     move.l    a6,a5
  645.  
  646.     moveq    #MN_SIZE+MP_SIZE,d0
  647.     move.l    #MEMF_CLEAR!MEMF_PUBLIC,d1
  648.     lib    Exec,AllocMem
  649.     tst.l    d0
  650.     beq.s    sendreq_end
  651.     move.l    d0,a2
  652.  
  653.     move.l    d5,LN_NAME(a2)
  654.  
  655.     moveq    #-1,d0
  656.     lib    AllocSignal
  657.     move.b    d0,MN_SIZE+MP_SIGBIT(a2)
  658.     bmi.s    sendreq_freemsg
  659.  
  660.     suba.l    a1,a1
  661.     lib    FindTask
  662.     move.l    d0,MN_SIZE+MP_SIGTASK(a2)
  663.  
  664.     lea    MN_SIZE(a2),a0
  665.     move.l    a0,MN_REPLYPORT(a2)
  666.  
  667.     lea    MN_SIZE+MP_MSGLIST(a2),a0
  668.     NEWLIST    a0
  669.  
  670.     move.l    crd_DosCallerPort(a5),a0
  671.     move.l    a2,a1
  672.     lib    PutMsg
  673.     lea    MN_SIZE(a2),a0
  674.     lib    WaitPort    
  675.  
  676.     moveq    #0,d0
  677.     move.b    MN_SIZE+MP_SIGBIT(a2),d0
  678.     lib    FreeSignal
  679.  
  680. sendreq_freemsg
  681.     move.l    a2,a1
  682.     moveq    #MN_SIZE+MP_SIZE,d0
  683.     lib    FreeMem
  684. sendreq_end
  685.     movem.l    (sp)+,d5/a2/a5-a6
  686.     rts
  687.  
  688. ;
  689. ; the dos caller process keeps DosBase in d7
  690. ; this macro is used to call DOS
  691. ;
  692. calldos    macro
  693.     exg    d7,a6
  694.     lib    \1
  695.     exg    d7,a6
  696.     endm
  697.  
  698. ;
  699. ; the dos caller process. only this process uses unit crdu_FileHandle fields
  700. ;
  701.  
  702. ; a fake seglist for CreateProc()
  703.     cnop    0,4
  704.     dc.l    20
  705. doscallerseglist
  706.     dc.l    0
  707.  
  708. doscaller_start
  709.     suba.l    a1,a1
  710.     lib    Exec,FindTask
  711.     move.l    d0,a3
  712.     lea    pr_MsgPort(a3),a0
  713.     lib    WaitPort        wait for startup message
  714.     move.l    d0,a1
  715.     move.l    d0,a2
  716.     lib    Remove            remove the message from port
  717.     move.l    LN_NAME(a2),a5        get device base address
  718.     move.l    crd_DosLib(a5),d7    get DosBase
  719.     move.l    crd_IntuitionBase(a5),d6
  720.     move.l    crd_DosCallerPort(a5),a5    get message port address
  721.  
  722.     move.l    a3,MP_SIGTASK(a5)
  723.     moveq    #-1,d0
  724.     lib    AllocSignal
  725.     move.b    d0,MP_SIGBIT(a5)
  726.     clr.b    MP_FLAGS(a5)        set flags to PA_SIGNAL
  727.  
  728.     bra.s    proc_msgloop
  729.  
  730. proc_wait
  731.     moveq    #0,d0
  732.     move.b    MP_SIGBIT(a5),d1
  733.     bset    d1,d0
  734.     lib    Wait
  735.  
  736. proc_msgloop
  737.     move.l    a5,a0
  738.     lib    GetMsg
  739.     tst.l    d0
  740.     beq.s    proc_wait
  741.     move.l    d0,a4
  742.     move.l    LN_NAME(a4),d0
  743.     beq    quit_proc
  744.     move.l    d0,a3            get IORequest pointer to a3
  745.     move.l    IO_UNIT(a3),a2        get unit pointer to a2
  746.     cmp.w    #CMD_RESET,IO_COMMAND(a3)
  747.     beq    proc_reset        this doesn't need a valid key
  748.  
  749.     tst.b    crdu_KeyValid(a2)
  750.     bne.b    key_valid
  751. ;
  752.     bsr    AskPassword
  753. ;
  754.  
  755. key_valid
  756.     move.w    IO_COMMAND(a3),d0
  757.     and.w    #$7fff,d0        mask out EXTCOM flag bit
  758.     cmp.w    #CMD_RESET,d0        request to close file ?
  759.     beq    proc_reset
  760.     cmp.w    #CMD_READ,d0        if not read then write
  761.     bne    do_write        write may be CMD_WRITE or TD_FORMAT
  762.  
  763.     bsr    openfile
  764.     tst.l    d0
  765.     beq    proc_error
  766.  
  767.     bsr    seekfile
  768.     tst.l    d0
  769.     bmi    proc_error
  770. ;
  771. ; read
  772. ;
  773.     move.l    crdu_FileHandle(a2),d1
  774.     move.l    IO_DATA(a3),d2
  775.     move.l    IO_LENGTH(a3),d3
  776.     calldos    Read
  777.     tst.l    d0
  778.     bmi    proc_error
  779.     move.l    d0,IO_ACTUAL(a3)
  780.     move.l    d0,d3
  781. ;
  782. ; decrypt
  783. ;
  784.     move.l    d6,-(sp)
  785.     move.l    #BLOCKSIZE,d5
  786.     move.l    IO_OFFSET(a3),d6
  787.  
  788. decrypt_loop
  789.     move.l    d6,crdu_IVec(a2)
  790.     move.l    d6,crdu_IVec+4(a2)
  791.  
  792.     add.l    d5,d6
  793.  
  794.     clr.l    -(sp)        ;decrypt
  795.     pea    crdu_IVec(a2)
  796.     pea    crdu_KeySched(a2)
  797.     move.l    d5,-(sp)
  798.     move.l    d2,-(sp)
  799.     move.l    d2,-(sp)
  800.     bsr    _idea_cbc_encrypt
  801.     lea    6*4(sp),sp
  802.     add.l    d5,d2
  803.     sub.l    d5,d3
  804.     bgt.b    decrypt_loop
  805.     move.l    (sp)+,d6
  806.     bra    proc_replymsg
  807.  
  808. do_write
  809.     bsr    openfile
  810.     tst.l    d0
  811.     beq    proc_error
  812.  
  813.     bsr    seekfile
  814.     tst.l    d0
  815.     bmi    proc_error
  816.  
  817.     move.l    d6,-(sp)
  818.     move.l    #BLOCKSIZE,d5
  819.     move.l    IO_DATA(a3),d2
  820.     move.l    IO_LENGTH(a3),d3
  821.     clr.l    IO_ACTUAL(a3)
  822.     move.l    IO_OFFSET(a3),d6
  823.  
  824. encrypt_loop
  825.     move.l    d6,crdu_IVec(a2)
  826.     move.l    d6,crdu_IVec+4(a2)
  827.  
  828.     add.l    d5,d6
  829.  
  830.     pea    1        ;encrypt
  831.     pea    crdu_IVec(a2)
  832.     pea    crdu_KeySched(a2)
  833.     move.l    d5,-(sp)
  834.     pea    crdu_Buffer(a2)
  835.     move.l    d2,-(sp)
  836.     bsr    _idea_cbc_encrypt
  837.     lea    6*4(sp),sp
  838.  
  839.     move.l    crdu_FileHandle(a2),d1
  840.     lea    crdu_Buffer(a2),a0
  841.     movem.l    d2/d3,-(sp)
  842.     move.l    a0,d2
  843.     move.l    d5,d3
  844.     calldos    Write
  845.     movem.l    (sp)+,d2/d3
  846.     tst.l    d0
  847.     bmi.b    proc_error1
  848.  
  849.     add.l    d5,d2
  850.     add.l    d5,IO_ACTUAL(a3)
  851.     sub.l    d5,d3
  852.     bgt.b    encrypt_loop
  853.  
  854.     move.l    (sp)+,d6
  855.  
  856. proc_replymsg
  857.     move.l    a4,a1
  858.     lib    ReplyMsg
  859.     bra    proc_msgloop
  860.  
  861. proc_error1
  862.     move.l    (sp)+,d6
  863.  
  864. proc_error
  865.     calldos    IoErr
  866.     tst.b    d0
  867.     beq.s    01$
  868.     moveq    #-10,d0
  869. 01$    move.b    d0,IO_ERROR(a3)
  870.     clr.l    IO_ACTUAL(a3)
  871.     bra.s    proc_replymsg
  872.  
  873. proc_reset
  874.     bsr.s    closefile
  875.     bra.s    proc_replymsg
  876.  
  877. openfile
  878.     move.l    crdu_FileHandle(a2),d0
  879.     bne.s    opf9
  880.     lea    crdu_FileName(a2),a0
  881.     move.l    a0,d1
  882.     move.l    #MODE_OLDFILE,d2
  883.     calldos    Open
  884.     move.l    d0,crdu_FileHandle(a2)
  885. opf9    rts
  886.  
  887. seekfile
  888.     move.l    crdu_FileHandle(a2),d1
  889.     move.l    IO_OFFSET(a3),d2
  890.     moveq    #OFFSET_BEGINNING,d3
  891.     calldos    Seek
  892.     rts
  893.  
  894. closefile
  895.     move.l    crdu_FileHandle(a2),d1
  896.     beq.s    clf9
  897.     calldos    Close
  898.     clr.l    crdu_FileHandle(a2)
  899. clf9    rts
  900.  
  901. ;
  902. ; quit the DosCaller process (just return from it...)
  903. ;
  904. quit_proc
  905.     move.l    a4,a1
  906.     jlib    ReplyMsg
  907.  
  908. ;
  909. ; ask password from the user
  910. ;
  911. AskPassword
  912.     movem.l    d2/d3/a4/a3/a6,-(sp)
  913.  
  914.     moveq    #0,d2
  915.  
  916.     lea    crdu_Buffer(a2),a0
  917.     suba.l    a1,a1
  918.     move.l    #sc_BarHeight+1,d0
  919.     moveq    #WBENCHSCREEN,d1
  920.     move.l    d6,a6            ;intuitionbase
  921.     lib    GetScreenData
  922.     tst.l    d0
  923.     beq.b    0$
  924.  
  925.     move.b    crdu_Buffer+sc_BarHeight(a2),d2
  926.  
  927. 0$    lea    NewWin(pc),a0
  928.     lea    crdu_Buffer(a2),a1
  929.     moveq    #nw_SIZEOF,d0
  930.     lib    Exec,CopyMem
  931.  
  932.     add.w    d2,crdu_Buffer+nw_Height(a2)
  933.  
  934.     move.l    a2,-(sp)
  935.     lea    passwd_format(pc),a0
  936.     lea    crdu_UnitNum(a2),a1
  937.     lea    crdu_Buffer+400(a2),a3
  938.     lea    putch(pc),a2
  939.     lib    RawDoFmt
  940.     move.l    (sp)+,a2
  941.  
  942.     move.l    d6,a6
  943.     lea    crdu_Buffer(a2),a0
  944.     lib    OpenWindow
  945.     tst.l    d0
  946.     beq    askpw_end
  947.     move.l    d0,a4
  948.  
  949.     move.l    a2,-(sp)
  950.     move.l    a4,a0
  951.     lea    crdu_Buffer+400(a2),a1
  952.     lea    -1,a2
  953.     lib    SetWindowTitles
  954.     move.l    (sp)+,a2
  955.  
  956. enter_pass
  957.     lea    enterpass_txt(pc),a0
  958.     moveq    #10,d0
  959.     moveq    #8,d1
  960.     bsr    showtext
  961.  
  962.     bsr    get_input_line
  963.  
  964.     lea    crdu_Buffer(a2),a0
  965.     move.l    a0,d0
  966.     lea    crdu_Buffer+128(a2),a1
  967. 1$    move.b    (a0)+,(a1)+        ;make a copy of the passphrase
  968.     bne.b    1$
  969.  
  970.     suba.l    d0,a0
  971.     cmpa.w    #11,a0            ;minimum passphrase length 10 chars
  972.     bcs.b    too_short
  973.  
  974.     lea    verifypass_txt(pc),a0
  975.     moveq    #10,d0
  976.     moveq    #8,d1
  977.     bsr    showtext
  978.  
  979.     bsr    get_input_line
  980.  
  981.     lea    crdu_Buffer(a2),a0
  982.     lea    crdu_Buffer+128(a2),a1
  983. 2$    cmpm.b    (a0)+,(a1)+
  984.     bne.b    verify_fail
  985.     tst.b    -1(a0)
  986.     bne.b    2$
  987. ;
  988. ; verify ok
  989. ;
  990.     movea.l    a4,a0
  991.     movea.l    d6,a6
  992.     lib    CloseWindow
  993.  
  994.     lea    crdu_Buffer+256(a2),a0
  995.     clr.l    (a0)+
  996.     clr.l    (a0)+
  997.     clr.l    (a0)+
  998.     clr.l    (a0)+
  999.  
  1000.     lea    crdu_Buffer(a2),a0
  1001.     lea    crdu_Buffer+256+16(a2),a3
  1002. passwd_l1
  1003.     lea    crdu_Buffer+256(a2),a1
  1004. passwd_loop
  1005.     move.b    (a0)+,d0
  1006.     beq.b    do_key_sched
  1007.     add.b    d0,(a1)+
  1008.     cmpa.l    a3,a1
  1009.     bcs.b    passwd_loop
  1010.     bra.b    passwd_l1
  1011.  
  1012. do_key_sched
  1013.     pea    crdu_KeySched(a2)
  1014.     pea    crdu_Buffer+256(a2)
  1015.     bsr    _idea_key_schedule
  1016.     addq.l    #8,sp
  1017.  
  1018. ;
  1019. ; remove password from memory
  1020. ;
  1021.     lea    crdu_Buffer(a2),a0
  1022.     moveq    #0,d0
  1023.     moveq    #(512/4)-1,d1
  1024. clr2_loop
  1025.     move.l    d0,(a0)+
  1026.     dbf    d1,clr2_loop
  1027.  
  1028.     st    crdu_KeyValid(a2)
  1029.  
  1030. askpw_end
  1031.     movem.l    (sp)+,d2/d3/a3/a4/a6
  1032.     rts
  1033.  
  1034. too_short
  1035.     lea    tooshort_txt(pc),a0
  1036.     bra.b    fail_comm
  1037.  
  1038. verify_fail
  1039.     lea    verifyfail_txt(pc),a0
  1040.  
  1041. fail_comm
  1042.     moveq    #10,d0
  1043.     moveq    #8,d1
  1044.     bsr    showtext
  1045.  
  1046.     move.l    wd_WScreen(a4),a0
  1047.     move.l    d6,a6
  1048.     lib    DisplayBeep
  1049.  
  1050.     move.l    d7,a6
  1051.     move.l    #2*50,d1    ;2 seconds
  1052.     lib    Delay
  1053.     bra    enter_pass
  1054.  
  1055. get_input_line
  1056.     lea    crdu_Buffer(a2),a3
  1057.  
  1058. msgloop    move.l    wd_UserPort(a4),a0
  1059.     lib    Exec,WaitPort
  1060.     move.l    wd_UserPort(a4),a0
  1061.     lib    GetMsg
  1062.     tst.l    d0
  1063.     beq.b    msgloop
  1064.     move.l    d0,a1
  1065.     move.l    im_Class(a1),d2
  1066.     move.w    im_Code(a1),d3
  1067.     lib    ReplyMsg
  1068.     cmp.l    #VANILLAKEY,d2
  1069.     bne.s    msgloop
  1070.  
  1071.     cmp.b    #8,d3        ;backspace?
  1072.     beq.b    pw_del
  1073.     cmp.b    #127,d3        ;del?
  1074.     bne.b    no_del
  1075.  
  1076. pw_del    lea    crdu_Buffer(a2),a0
  1077.     cmp.l    a0,a3
  1078.     beq.b    do_beep        ;can't delete if no chars in buffer
  1079.  
  1080.     subq.l    #1,a3
  1081.     lea    space_txt(pc),a0
  1082.     bsr.b    point
  1083.     bra.b    msgloop
  1084.  
  1085. no_del    cmp.b    #13,d3        ;CR?
  1086.     beq.b    getline_done
  1087.  
  1088.     lea    crdu_Buffer+125(a2),a0
  1089.     cmp.l    a0,a3
  1090.     bcs.b    add_char
  1091.  
  1092. do_beep    move.l    wd_WScreen(a4),a0
  1093.     move.l    d6,a6
  1094.     lib    DisplayBeep
  1095.     bra.b    msgloop
  1096.  
  1097. add_char
  1098.     lea    point_txt(pc),a0
  1099.     bsr.b    point
  1100.  
  1101.     move.b    d3,(a3)+
  1102.     bra    msgloop
  1103.  
  1104. getline_done
  1105.     clr.b    (a3)
  1106.     moveq    #10,d0
  1107.     moveq    #20,d1
  1108.     lea    spaces_txt(pc),a0
  1109.     bsr    showtext
  1110.     moveq    #10,d0
  1111.     moveq    #28,d1
  1112.     lea    spaces_txt(pc),a0
  1113.     bsr    showtext
  1114.     moveq    #10,d0
  1115.     moveq    #36,d1
  1116.     lea    spaces_txt(pc),a0
  1117.     bsr    showtext
  1118.     moveq    #10,d0
  1119.     moveq    #44,d1
  1120.     lea    spaces_txt(pc),a0
  1121.     bra    showtext
  1122.  
  1123. ;
  1124. ;
  1125. ;
  1126. point    lea    crdu_Buffer(a2),a1
  1127.     suba.l    a3,a1
  1128.     move.l    a1,d0
  1129.     neg.l    d0
  1130.     move.l    d0,d1
  1131.     and.w    #%11111,d0
  1132.     lsr.w    #5,d1
  1133.     lsl.l    #3,d0
  1134.     add.w    #10,d0
  1135.     lsl.l    #3,d1
  1136.     add.w    #20,d1
  1137. ;    bra    showtext
  1138. ; drop to showtext
  1139.  
  1140. ;
  1141. ; text in a0..., x/y coords in d0/d1 (upper word must be zero)
  1142. ;
  1143. showtext
  1144.     swap    d0
  1145.     or.l    d0,d1
  1146.  
  1147.     move.l    a0,d0
  1148. ;
  1149. ; use the end of crdu_Buffer for an IntuiText structure
  1150. ;
  1151.     lea    crdu_Buffer+BLOCKSIZE-it_SIZEOF(a2),a0
  1152.     move.l    #(1<<24)!(0<<16)!(RP_JAM2<<8)!0,(a0)+    ;pens, drawmode
  1153.     move.l    d1,(a0)+            ;left, top
  1154.     lea    Topaz80(pc),a1
  1155.     move.l    a1,(a0)+        ;font
  1156.     move.l    d0,(a0)+        ;text
  1157.     clr.l    (a0)            ;next
  1158.     move.l    d6,a6            ;intutionbase
  1159.     move.l    wd_RPort(a4),a0
  1160.     lea    crdu_Buffer+BLOCKSIZE-it_SIZEOF(a2),a1
  1161.     moveq    #0,d0
  1162.     moveq    #0,d1
  1163.     move.b    wd_BorderLeft(a4),d0
  1164.     move.b    wd_BorderTop(a4),d1
  1165.     lib    PrintIText
  1166.     rts
  1167.  
  1168. NewWin    dc.w    100,100,300,70
  1169.     dc.b    -1,-1
  1170.     dc.l    VANILLAKEY
  1171.     dc.l    WINDOWDEPTH!WINDOWDRAG!ACTIVATE!SMART_REFRESH!NOCAREREFRESH
  1172.     dc.l    0
  1173.     dc.l    0
  1174.     dc.l    dev_name    ;title
  1175.     dc.l    0,0    ;no screen, no bitmap
  1176.     dc.w    0,0,0,0
  1177.     dc.w    WBENCHSCREEN
  1178.  
  1179. Topaz80    dc.l    topaz_name
  1180.     dc.w    8
  1181.     dc.b    FS_NORMAL
  1182.     dc.b    FPF_ROMFONT
  1183.  
  1184. topaz_name    dc.b    'topaz.font',0
  1185.  
  1186. enterpass_txt    dc.b    'Enter pass phrase    ',0
  1187. verifypass_txt    dc.b    'Retype pass phrase   ',0
  1188. verifyfail_txt    dc.b    'Pass phrases differ  ',0
  1189. tooshort_txt    dc.b    'Pass phrase too short',0
  1190. passwd_format    dc.b    'cryptdev unit #%d',0
  1191. point_txt    dc.b    '.',0
  1192. space_txt    dc.b    ' ',0
  1193. spaces_txt    dc.b    '                                 ',0
  1194.  
  1195. ;
  1196. ; label for RT_ENDSKIP -- note that this must be in the same hunk as RomTag
  1197. ;
  1198. EndSkip
  1199.  
  1200.     end
  1201.